<?php

class PresentacionController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
     	# $this->_helper->layout->disableLayout ();
		//$this->_helper->viewRenderer->setNoRender ();
		 echo "indexAction";
	  
    }
	
	public function presentacionAction(){
		echo $this->view->util()->registerScriptJSController($this->getRequest());
	}
	public function buscarAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('tip');
			$val = $upper->filter($this->_request->getPost('val'));
			$chk = $this->_request->getPost('chk');
			$cn = new Model_Data();
			$parameter[]=array("@pcod_present",($tip=='0' ? $val:''));
			$parameter[]=array("@pdesc_nom",($tip=='1' ? $val:''));
			$parameter[]=array("@pest_present",$chk);
			$result_bus = $cn ->ejec_store_procedura_sql("MPresentacion_list", $parameter);
	

			if($result_bus==null || count($result_bus)==0){
				echo "Sin Resultados";
			}else{
				$tb="";
				foreach ($result_bus as $value) {
					$tb .= '<tr id="'.$value[0].'">';
					$tb .='<td>'.$value[0].'</td>';
					$tb .='<td>'.$value[1].'</td>';
					$tb .='<td>'.$value[2].'</td>';
					$tb .= '</tr>';
				}
				//echo '<table border="1"><tr><td>';
				echo '<table id="tbl_result" class="ui-widget ui-widget-content tablesorter">'.
						'<thead>'.
							'<tr class="ui-widget-header">'.
								'<tr class="ui-widget-header">'.
								'<th width="100px">Codido</th>'.
								'<th width="279px">Descripcion</th>'.
								'<th width="120px">Abr.</th>'.
								'</tr>'.
							'</tr>'.
						'</thead>'.
						'<tbody>'.
							$tb.
						'</tbody>'.
					'</table>';

				//echo "</td></tr><tr><td>";
				echo $this->view->TableSorter()->ScriptDivPage();
				//echo $this->view->util()->registerScriptJSController($this->getRequest());
				//echo "</td></tr></table>";
				//$(\"#tbl_result\").tablesorter({sortList:[[0,0]], widgets: ['zebra'],headers: { 5:{sorter: false}}}).tablesorterPager({container: $(\"#pager\")});
				echo "<script type=\"text/javascript\">
						$(function() {		
							$(\"#tbl_result\").tablesorter({sortList:[[0,0]]})
							$(\"#tbl_result\").tablesorterPager({
								container: $(\"#pager\"),
								afterPagingCallBack: function(){
									AbrirMenu();
								}
								});
							
						});	
					</script>";
				
				
			}
			
		}
	}
	
	public function mantepresentAction(){
		
     	echo $this->view->util()->registerScriptJSController($this->getRequest());
		$this->_helper->layout->disableLayout ();
		#$this->_helper->viewRenderer->setNoRender ();
		$type = $this->_request->getParam('type', '');
		$cod = $this->_request->getParam('codItem', '');
		
		$cn = new Model_Data();
		$pintar = New Libreria_Pintar();
		$parameter[]=array("@pcod_present",$cod);
		$parameter[]=array("@pdesc_nom",'');
		$parameter[]=array("@pest_present",'');
		
		if ($type=='N'){

		}elseif($type=='M' || $type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("MPresentacion_list", $parameter);
			$val[] = array('#hdcod_present',$result_bus[0][0],'val');
			$val[] = array('#txtds_descrip',$result_bus[0][1],'val');
			$val[] = array('#txtds_abr',$result_bus[0][2],'val');
			$pintar->PintarValor($val);
		}
	}

	public function executemanteAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('type');
			$cod_present = $upper->filter($this->_request->getPost('cod_present'));
			$ds_descrip = $upper->filter($this->_request->getPost('ds_descrip'));
			$ds_abr = $upper->filter($this->_request->getPost('ds_abr'));
			$st_estado = $upper->filter($this->_request->getPost('st_stado'));
			
			$cn = new Model_Data();
			
			if($tip=='M' ||$tip=='N' ){
				$parameter[]=array("@ptype",$tip);
				$parameter[]=array("@pcod_present",$cod_present);
				$parameter[]=array("@pdesc_nom",$ds_descrip);
				$parameter[]=array("@pdesc_abr",$ds_abr);
				$parameter[]=array("@pest_present" ,$st_estado);
					/*@pcod_present		char(5),
					@pdesc_nom		varchar(150),
					@pdesc_abr	char(5),
					@pest_present*/
				$parameter[]=array(	"@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("MPresentacion_InsertUpdate", $parameter);
				 	
			}elseif($tip=='D'){
	
				$parameter[]=array("@pcod_present",$cod_present);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("MPresentacion_Delete", $parameter);
			}
			if($result[0][0]=='1') echo "Transaccion Exitosa";
			if($result[0][0]=='2') 
			 echo  $this->view->util()->getTextError($result[0]);
		}
		
	}

}

